# -*- coding: utf-8 -*-

import gettext
import __builtin__

from w.settings import W
from w.i18n.gettext.processor import parseDomain

def useTranslation(domain,lang=None,localedir=None,codeset='UTF-8'):
    lroot = localedir or W.I18N.ROOT_DIR
    lang = lang or W.I18N.DEFAULT_LANG
    domain,ldir = parseDomain(domain)
    try:
        l = gettext.translation(domain,
                                localedir='%s/%s'%(lroot,ldir),
                                languages=[lang],
                                codeset=codeset)
        l.install(names=W.I18N.TRANSLATION_FUNC_NAMES)
    except Exception,ex:
        for n in W.I18N.TRANSLATION_FUNC_NAMES + ['_',]:
            __builtin__.__dict__[n] = W.I18N.RESOURCE_ERROR_CALLBACK(ex)
